<template>
  <el-dialog title="分配角色" :visible="isShowRoleDialog" @close="btnCancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkList">
      <!-- 选项 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        <template>{{ item.name }}</template>
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleListAPI } from '@/api/setting'
import { assignRoles } from '@/api/employees'
import { getUserDetailById } from '@/api/user'

export default {
  props: {
    isShowRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      // 获取的所有角色列表
      roleList: [],
      //   角色选中的列表
      checkList: [],
      //   被点击的员工id
      RoleId: ''
    }
  },
  async created() {
    const { rows } = await getRoleListAPI({
      page: 1,
      pagesize: 100
    })
    this.roleList = rows
  },
  methods: {
    // 获取员工个人信息
    async getUserDetail(id) {
      const { roleIds } = await getUserDetailById(id)
      this.checkList = roleIds
      this.RoleId = id
    },
    async btnOK() {
      await assignRoles({
        id: this.RoleId,
        roleIds: this.checkList
      })
      this.$message.success('恭喜，设置成功！！')
      this.btnCancel()
    },
    btnCancel() {
      this.$emit('closeRoleDialog')
    }
  }
}
</script>
